package com.ruoyi.oc.service.impl;

import com.ruoyi.oc.domain.SalesOrderDetail;
import com.ruoyi.oc.mapper.SalesOrderDetailMapper;
import com.ruoyi.oc.service.ISalesReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售报表服务实现类
 *
 * @author ruoyi
 * @date 2025-06-18
 */
@Service
public class SalesReportServiceImpl implements ISalesReportService {

    @Autowired
    private SalesOrderDetailMapper salesOrderDetailMapper;

    @Override
    public Map<String, Long> getGoodsSalesQuantity() {
        List<SalesOrderDetail> details = salesOrderDetailMapper.selectSalesOrderDetailList(new SalesOrderDetail());
        Map<String, Long> quantityMap = new HashMap<>();
        for (SalesOrderDetail detail : details) {
            String goodsId = detail.getGoodsId();
            Long quantity = detail.getQuantity();
            quantityMap.put(goodsId, quantityMap.getOrDefault(goodsId, 0L) + quantity);
        }
        return quantityMap;
    }

    @Override
    public Map<String, Double> getOrderSalesAmount() {
        List<SalesOrderDetail> details = salesOrderDetailMapper.selectSalesOrderDetailList(new SalesOrderDetail());
        Map<String, Double> amountMap = new HashMap<>();
        for (SalesOrderDetail detail : details) {
            String orderId = detail.getOrderId();
            BigDecimal unitPrice = detail.getUnitPrice();
            Long quantity = detail.getQuantity();
            double amount = unitPrice.doubleValue() * quantity;
            amountMap.put(orderId, amountMap.getOrDefault(orderId, 0.0) + amount);
        }
        return amountMap;
    }

    @Override
    public Map<String, Double> getDailySalesTotal() {
        // 这里假设可以从 SalesOrderDetail 中获取日期信息，实际情况可能需要调整
        List<SalesOrderDetail> details = salesOrderDetailMapper.selectSalesOrderDetailList(new SalesOrderDetail());
        Map<String, Double> dailyTotalMap = new HashMap<>();
        for (SalesOrderDetail detail : details) {
            // 假设 createTime 是日期信息，需要根据实际情况调整
            String date = detail.getCreateTime().toString();
            BigDecimal unitPrice = detail.getUnitPrice();
            Long quantity = detail.getQuantity();
            double amount = unitPrice.doubleValue() * quantity;
            dailyTotalMap.put(date, dailyTotalMap.getOrDefault(date, 0.0) + amount);
        }
        return dailyTotalMap;
    }
}